package org.deadline.model.baseapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.config.Guid;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@OnDelete(action=OnDeleteAction.CASCADE)
public class Principal extends BaseEntity  implements Serializable {

    @Embedded
    private Guid guid;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "principal")
    private Set<CommunicationChannel> communicationChannels = new HashSet<CommunicationChannel>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "principal")
    private Set<Address> addresses = new HashSet<Address>();

	@Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="principal_seq_gen",sequenceName="principal_seq")
	@GeneratedValue(generator="principal_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;

	
	public Guid getGuid() {
        return this.guid;
    }

	public void setGuid(Guid guid) {
        this.guid = guid;
    }

	public Set<CommunicationChannel> getCommunicationChannels() {
        return this.communicationChannels;
    }

	public void setCommunicationChannels(Set<CommunicationChannel> communicationChannels) {
        this.communicationChannels = communicationChannels;
    }

	public Set<Address> getAddresses() {
        return this.addresses;
    }

	public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	
	public Long getId() {
	    return this.id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	public static Principal fromJsonToPrincipal(String json) {
        return new JSONDeserializer<Principal>().use(null, Principal.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Principal> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<Principal> fromJsonArrayToPrincipals(String json) {
        return new JSONDeserializer<List<Principal>>().use(null, ArrayList.class).use("values", Principal.class).deserialize(json);
    }

	private static final long serialVersionUID = 1L;
}
